package edu.nwpu.controller;

import com.alibaba.fastjson.JSON;
import edu.nwpu.pojo.FoodType;
import edu.nwpu.service.FoodTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

@RestController
public class FoodTypeController {

    @Resource
    private FoodTypeService foodTypeService;

//    @GetMapping("/foodType/query/id/{id}")
//    public String getFoodTypeById(@PathVariable("id") Long id) {
//        Map<String, Object> response = new HashMap<>();
//        FoodType type = foodTypeService.getFoodTypeById(id);
//        if (null == type) {
//            response.put("status", "failed");
//        } else {
//            response.put("status", "success");
//            response.put("info", type);
//        }
//        return JSON.toJSONString(response);
//    }

    @ApiOperation(value = "查询所有的菜品类型", httpMethod = "GET", response = String.class,
            notes = "返回json")
    @GetMapping("/foodType/list/all")
    public String getAllFoodTypes() {
        Map<String, Object> response = new HashMap<>();
        List<FoodType> types = foodTypeService.getAllFoodTypes();
        if (types == null || types.size() == 0) {
            response.put("status", "failed");
        } else {
            response.put("status", "success");
            response.put("data", types);
        }
        return JSON.toJSONString(response);
    }

    @PostMapping("/manage/foodType/update")
    @Deprecated
    public String updateFoodType(FoodType foodType) {
        boolean status = foodTypeService.updateFoodType(foodType);
        Map<String, Object> response = new HashMap<>();
        response.put("status", status ? "success" : "failed");
        return JSON.toJSONString(response);
    }

    @GetMapping("/manager/foodType/delete/id/{id}")
    @Deprecated
    public String deleteFoodTypeById(@PathVariable("id") Long id) {
        boolean status = foodTypeService.deleteFoodTypeById(id);
        Map<String, Object> response = new HashMap<>();
        response.put("status", status ? "success" : "failed");
        return JSON.toJSONString(response);
    }

//    @GetMapping("/foodType/delete/name/{name}")
//    public String deleteFoodTypeByName(@PathVariable("name") String name) {
//        boolean status = foodTypeService.deleteFoodTypeByName(name);
//        Map<String, Object> response = new HashMap<>();
//        response.put("status", status ? "success" : "failed");
//        return JSON.toJSONString(response);
//    }
}
